package cn.lijiajia3515.cairo.security.web.authentication;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.web.accept.ContentNegotiationManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * security 认证异常 处理器
 * <p>异常message 输出到 response</p>
 */
@Slf4j
public class CairoHttpMessageAuthenticationFailedHandler implements AuthenticationFailureHandler {
	protected final CairoHttpMessageAuthenticationEntryPoint entryPoint;

	public CairoHttpMessageAuthenticationFailedHandler(List<HttpMessageConverter<?>> converters) {
		entryPoint = new CairoHttpMessageAuthenticationEntryPoint(converters);
	}

	public CairoHttpMessageAuthenticationFailedHandler(List<HttpMessageConverter<?>> converters, ContentNegotiationManager contentNegotiationManager) {
		entryPoint = new CairoHttpMessageAuthenticationEntryPoint(converters, contentNegotiationManager);
	}

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
		entryPoint.commence(request, response, exception);
	}

}
